package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.DepartmentMapper;
import com.example.model.dtos.PageResult;
import com.example.model.pojo.Department;
import com.example.model.pojo.Materials;
import com.example.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired(required = false)
    DepartmentMapper departmentMapper;

    @Override
    public Boolean addDepartment(Department department) {
        int insert = departmentMapper.insert(department);
        return insert !=0;
    }

    @Override
    public Boolean deleteDepartment(String id) {
        int insert = departmentMapper.deleteById(id);
        return insert !=0;
    }

    @Override
    public PageResult DepartmentList(Integer pageStart, Integer pageSize) {
        //创建分页对象并将当前页码和页面大小写入
        Page<Department> departmentPage =new Page<>(pageStart, pageSize);
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        departmentMapper.selectPage(departmentPage, queryWrapper);
        List<Department> records = departmentPage.getRecords();

//        返回一个分页后的结果
        PageResult pageResult = new PageResult(pageStart.longValue(), pageSize.longValue(), departmentPage.getTotal(),records);

        return pageResult;
    }

    @Override
    public Boolean updateDepartment(Department department) {
        int insert = departmentMapper.updateById(department);
        return insert !=0;

    }
}
